#include "hotkeytablemodel.h"
#include <QDebug>
#include <QMessageBox>

HotKeyTableModel::HotKeyTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
   // connect(this, SIGNAL(sig_dataChanged ( int , const int , const QString& )), parent, SIGNAL(sig_dataChanged ( int , const int , const QString& )));
}

QVariant HotKeyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    // FIXME: Implement me!
    if (orientation == Qt::Horizontal) {
        if(role == Qt::DisplayRole) {
            return m_headData.at(section);
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

int HotKeyTableModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    else
        return this->m_datas.size();

    // FIXME: Implement me!
}

int HotKeyTableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    else
        return m_headData.size();

    // FIXME: Implement me!
}

QVariant HotKeyTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    // FIXME: Implement me!
    if (role == Qt::DisplayRole) {
        return (m_datas.begin() + index.row())->at(index.column()); // 对应数据
    }

    // 对齐方式
    if(role == Qt::TextAlignmentRole) {
        return int(Qt::AlignLeft | Qt::AlignVCenter);
    }
    else if ( role == Qt::EditRole)
    {
//        switch ( index.column() )
//        {
//            //to do ...
//        }
    }

    return QVariant();
}

Qt::ItemFlags HotKeyTableModel::flags(const QModelIndex& index) const
{
    if(index.column() == 1) {

        return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
    }
    else
        return  QAbstractTableModel::flags(index);
}

bool HotKeyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid()){
        qDebug() << "index is valid: " << index.row() << ":" << index.column();
        return false;
    }
    if(role == Qt::EditRole){
        if(dataHandle( value.toString()) > -1 ) {
            QMessageBox::warning(NULL,  "警告",  "快捷键已经存在");

        } else if(dataHandle( value.toString()) == -2 ){
            auto item = m_datas.value(index.row());
            item[index.column()] = tr("None");
            item[index.column()+1] = tr("No set");
            m_datas[index.row()] = item;
        }else {
            auto item = m_datas.value(index.row());
            item[index.column()] = value.toString();
            item[index.column()+1] = tr("正常");
            m_datas[index.row()] = item;
        }

        emit dataChanged(index, index);
        return true;
    }
    return false;
}

int HotKeyTableModel::dataHandle(const QString& sKeyText) {

    if (sKeyText.isEmpty()) return -2;
    int index = -1;
    QMap<int, QVector<QString>>::iterator it = m_datas.begin();
    while(it != m_datas.end()) {
        if(it.value().at(1) == sKeyText) {
            index = it.key();
            break;
        }
        it++;
    }
//    for ( auto const &pair : m_datas) {
//        qDebug() << pair.
////        if(pair.value().at(1) == skeyText) {
////            index = pair.first();
////            break;
////        }
//    }

    return index;
}
